<?php
class item_changegroup extends template_item
{
	var $default_name = "Change Group";
	var $default_description = "Change your group to {group_name}";
	
	var $able_to_select_admin = 0;
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	var $description = "";
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['group_id'] = $this->ipsclass->vars['member_group'];
		}
		
		$this->description = str_replace('{group_name}',$this->ipsclass->cache['group_cache'][$this->item_config['group_id']]['g_title'],$this->item['item_description']);
	}
	
	function admin_form()
	{
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $group_id => $group)
		{
			if($group_id == $this->ipsclass->vars['guest_group'] || $group_id == $this->ipsclass->vars['auth_group'] || !$group['g_view_board'] || !$group['g_market_view'] || !$group['g_market_use'])
			{
				continue;
			}
			
			if(!$group['g_access_cp'] || $this->able_to_select_admin)
			{
				$groups[] = array($group_id,$group['g_title']);
			}
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"Tags for item name and description",
															"{group_name} = The name of the group",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Change to which group?</b>",
															$this->ipsclass->adskin->form_dropdown("item[".$this->item['item_id']."][group_id]", $groups, $this->item_config['group_id']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['group_id'] = intval($input['group_id']);
		if($this->ipsclass->input['code'] != 'convert')
		{
			if(!is_array($this->ipsclass->cache['group_cache'][$save['group_id']]))
			{
				$this->ipsclass->admin->error("Invalid group");
			}
			if($save['group_id'] == $this->ipsclass->vars['guest_group'] || $save['group_id'] == $this->ipsclass->vars['auth_group'])
			{
				$this->ipsclass->admin->error("Group cannot be guest or validating");
			}
			if(!$this->ipsclass->cache['group_cache'][$save['group_id']]['g_view_board'] || !$this->ipsclass->cache['group_cache'][$save['group_id']]['g_market_view'] || !$this->ipsclass->cache['group_cache'][$save['group_id']]['g_market_use'])
			{
				$this->ipsclass->admin->error("Group must be able to view the board and market and use the market");
			}
			if($this->ipsclass->cache['group_cache'][$save['group_id']]['g_access_cp'] && !$this->able_to_select_admin)
			{
				$this->ipsclass->admin->error("You cannot select a group that have access to the ACP");
			}
		}
		
		$item_info['item_config'] = serialize($save);
		
		$item_info['item_description'] = str_replace("#group#", $this->ipclass->cache['group_cache'][$save['group_id']]['g_title'], $item_info['item_description']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		if(!is_array($this->ipsclass->cache['group_cache'][$this->item_config['group_id']]))
		{
			$this->ipsclass->Error(array('MSG'=>'invalid_group'));
		}
		
		if($this->item_config['group_id'] == $this->ipsclass->member['mgroup'])
		{
			$this->ipsclass->Error(array('MSG'=>'you_in_group'));
		}
		
		$this->ipsclass->DB->do_update('members',array(
														'mgroup' => $this->item_config['group_id'],
				),"id=".$this->ipsclass->member['id']);
		
		if(USE_MODULES == 1)
		{
			require ROOT_PATH."modules/ipb_member_sync.php";
			$modules = new ipb_member_sync();
			$modules->register_class(&$this);
			$modules->on_group_change($this->ipsclass->member['id'],$this->item_config['group_id']);
		}
		
		$this->ipsclass->lang['change_to_x_group'] = str_replace('<#GROUP#>', $this->ipsclass->cache['group_cache'][$this->item_config['group_id']]['g_title'], $this->ipsclass->lang['change_to_x_group']);
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Changed group from {$this->ipsclass->cache['group_cache'][$this->ipsclass->member['mgroup']]['g_title']} to {$this->ipsclass->cache['group_cache'][$this->item_config['group_id']]['g_title']}");
		
		$this->ipsclass->market_class->redirect('change_to_x_group');
	}
	
	function parse_store_description()
	{
		return $this->description;
	}
	
	function parse_group_description()
	{
		return $this->description;
	}
	
	function parse_single_description()
	{
		return $this->description;
	}
}
?>